package service.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;

import persistence.dao.TdfkfaDeclareUnitDAO;
import persistence.model.TdfkfaDeclareUnit;
import persistence.model.TdfkfaDeclareUnitExample;
import persistence.model.TdfkfaDeclareUnitExample.Criteria;
import service.UnitService;

public class UnitServiceImpl implements UnitService {

    private TdfkfaDeclareUnitDAO tdfkfaDeclareUnitDAO;

    public String saveUnit(TdfkfaDeclareUnit unit) {

        try {
            tdfkfaDeclareUnitDAO.insertSelective(unit);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    public String modifyUnit(TdfkfaDeclareUnit unit) {
        try {
            tdfkfaDeclareUnitDAO.updateByPrimaryKeySelective(unit);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    public List<TdfkfaDeclareUnit> getUnitList(int pageIndex, int pageSize) {
        TdfkfaDeclareUnitExample example = new TdfkfaDeclareUnitExample();
        List<TdfkfaDeclareUnit> list = tdfkfaDeclareUnitDAO.selectByExample(example, pageIndex, pageSize);
        return list;
    }

    public int countUnit() {
        TdfkfaDeclareUnitExample example = new TdfkfaDeclareUnitExample();
        return tdfkfaDeclareUnitDAO.countByExample(example);
    }

    public TdfkfaDeclareUnit getUnit(Integer unitId) {
        return tdfkfaDeclareUnitDAO.selectByPrimaryKey(unitId);
    }

    public void deleteUnit(Integer[] ids) {
        TdfkfaDeclareUnitExample example = new TdfkfaDeclareUnitExample();
        if (ids.length != 0) {
            for (Integer id : ids) {
                Criteria criteria = example.createCriteria().andUnitIdEqualTo(id);
                example.or(criteria);
            }
            tdfkfaDeclareUnitDAO.deleteByExample(example);
        }
    }

    public int updateUnit(TdfkfaDeclareUnit unit) {
        int ret = tdfkfaDeclareUnitDAO.updateByPrimaryKey(unit);
        return ret;
    }

    public void setTdfkfaDeclareUnitDAO(TdfkfaDeclareUnitDAO tdfkfaDeclareUnitDAO) {
        this.tdfkfaDeclareUnitDAO = tdfkfaDeclareUnitDAO;
    }

}
